<?php
class Vehiculo extends AppModel {

	var $name = 'Vehiculo';
	var $useTable = 'vehiculos';
	var $displayField = 'placas';		
	
	var $validate = array(
		'id' => VALID_NOT_EMPTY,
		'serie_motor' => VALID_NOT_EMPTY,
		'no_serie' => VALID_NOT_EMPTY,
		'placas' => VALID_NOT_EMPTY,
	);

	var $hasMany = array(
		'EstadoVehiculo' => array('className' => 'EstadoVehiculo',
							'foreignKey' => 'vehiculo_id',
							'conditions' => '',
							'fields' => '',
							'order' => '',
							'limit' => '',
							'offset' => '',
							'dependent' => '',
							'exclusive' => '',
							'finderQuery' => '',
							'counterQuery' => ''),
	
		'Siniestro' => array('className' => 'Siniestro',
							'foreignKey' => 'vehiculo_id',
							'conditions' => '',
							'fields' => '',
							'order' => '',
							'limit' => '',
							'offset' => '',
							'dependent' => '',
							'exclusive' => '',
							'finderQuery' => '',
							'counterQuery' => '')	
	);
	
	var $hasAndBelongsToMany = array(
		'Bodega' => array('className'    => 'Bodega',
                    'joinTable'    => 'bodegas_vehiculos',
                    'foreignKey'   => 'vehiculo_id',
                    'associationForeignKey'=> 'bodega_id',
                    'conditions'   => '',
                    'order'        => '',
					'limit'        => '',
                    'unique'       => true,
                    'finderQuery'  => '',
                    'deleteQuery'  => '',) ,
	
		'MantCor' => array('className'    => 'MantCor',
                    'joinTable'    => 'mant_cors_vehiculos',
                    'foreignKey'   => 'vehiculo_id',
                    'associationForeignKey'=> 'mant_cor_id',
                    'conditions'   => '',
                    'order'        => '',
					'limit'        => '',
                    'unique'       => true,
                    'finderQuery'  => '',
                    'deleteQuery'  => '',),

		'MantPrev' => array('className'    => 'MantPrev',
                    'joinTable'    => 'mant_prevs_vehiculos',
                    'foreignKey'   => 'vehiculo_id',
                    'associationForeignKey'=> 'mant_prev_id',
                    'conditions'   => '',
                    'order'        => '',
					'limit'        => '',
                    'unique'       => true,
                    'finderQuery'  => '',
                    'deleteQuery'  => '',),
	
		'CajaVehiculo' => array('className' => 'CajaVehiculo',
					'joinTable'    => 'caja_vehiculo_vehiculos',
					'foreignKey' => 'vehiculo_id',
					'associationForeignKey'=> 'caja_vehiculo_id',
					'conditions' => '',
					'fields' => '',
					'order' => '',
					'limit' => '',
					'offset' => '',
					'dependent' => '',
					'exclusive' => '',
					'finderQuery' => '',
					'counterQuery' => '')	
    );		

	var $hasOne = array(
		'Poliza' => array('className' => 'Poliza',
							'foreignKey' => 'vehiculo_id',
							'conditions' => '',
							'fields' => '',
							'order' => '',
							'limit' => '',
							'offset' => '',
							'dependent' => 'true',
							'exclusive' => '',
							'finderQuery' => '',
							'counterQuery' => ''),
	
		'VentaAuto' => array('className' => 'VentaAuto',
                              'conditions'   => '',
                              'order'        => '',
                              'dependent'    => '',
                              'foreignKey'   => ''
                              )		
	);

	var $belongsTo = array(
		'MarcaVehiculo' => array('className' => 'MarcaVehiculo',
							'foreignKey' => '',
							'conditions' => '',
							'fields' => '',
							'order' => '',
							'counterCache' => ''),
	
		'TipoVehiculo' => array('className' => 'TipoVehiculo',
							'foreignKey' => '',
							'conditions' => '',
							'fields' => '',
							'order' => '',
							'counterCache' => ''),
	
		'Proveedor' => array('className' => 'Proveedor',
							'foreignKey' => '',
							'conditions' => '',
							'fields' => '',
							'order' => '',
							'counterCache' => '')		
	);

	
	function buscar($palabraClave, $campoTabla){
		$resultado = $this->query("SELECT * FROM vehiculos, tipo_vehiculos WHERE $campoTabla = '$palabraClave' "); 
		return $resultado;
	}

	function updateKilometraje($data){
		$placas = $data['Vehiculo']['placas'];
		$km = $data['Vehiculo']['km'];
		$this->query("UPDATE vehiculos SET km = $km WHERE id = $placas");		
		return true;
	}
}
?>